package com.zzk.lanouhn.myapplication1.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.zzk.lanouhn.myapplication1.R;
import com.zzk.lanouhn.myapplication1.intefaces.NewsItemCliclListener;
import com.zzk.lanouhn.myapplication1.modles.Users;

/**
 * Created by lanouhn on 16/8/30.
 */
public class RecyclerViewPopupAdapter extends RecyclerView.Adapter<RecyclerViewPopupAdapter.ViewHolder> {

    private Context context;
    private Users users;
    private NewsItemCliclListener newsItemCliclListener;

    public void setRecyclerViewPopupAdapter(NewsItemCliclListener newsItemCliclListener) {
        this.newsItemCliclListener = newsItemCliclListener;
    }

    public RecyclerViewPopupAdapter(Context context, Users users) {
        this.context = context;
        this.users = users;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(context).inflate(R.layout.item_popupwindow, null);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder,  int position) {
        Users.DataBean.LatestArticleBean bean = users.getData().getLatestArticle().get(position);
        holder.tv_title.setText(bean.getTitle());
        Picasso.with(context).load(bean.getFeatureImg()).into(holder.iv_icon);
        if (newsItemCliclListener!=null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int posi=holder.getLayoutPosition();
                    newsItemCliclListener.onItemClick(posi);
                }
            });
        }
    }


    @Override
    public int getItemCount() {
        return users.getData().getLatestArticle().size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv_icon;
        private TextView tv_title;

        public ViewHolder(View itemView) {
            super(itemView);
            iv_icon = (ImageView) itemView.findViewById(R.id.iv_icon_popup_details);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title_popup_details);
        }
    }
}
